package small.common.executeEngine.task;

import java.util.concurrent.atomic.AtomicInteger;

public abstract class AbstractDelayTask implements ExecuteTask {
    /**
     * 任务间隔
     */
    private long taskInterval;

    /**
     * 最后一次执行时间
     */
    private long lastProcessTime;

    /**
     * 重试次数
     */
    private AtomicInteger retryTimes = new AtomicInteger(0);

    public AbstractDelayTask(long taskInterval) {
        this.taskInterval = taskInterval;
        this.lastProcessTime = System.currentTimeMillis();
    }

    public AbstractDelayTask(long taskInterval, long lastProcessTime) {
        this.taskInterval = taskInterval;
        this.lastProcessTime = lastProcessTime;
    }

    /**
     * 合并任务
     * @param task
     */
    public abstract void merge(AbstractDelayTask task);

    @Override
    public boolean shouldProcess() {
        return (System.currentTimeMillis() - this.lastProcessTime >= this.taskInterval);
    }

    public long getTaskInterval() {
        return taskInterval;
    }

    public void setTaskInterval(long taskInterval) {
        this.taskInterval = taskInterval;
    }

    public long getLastProcessTime() {
        return lastProcessTime;
    }

    public void setLastProcessTime(long lastProcessTime) {
        this.lastProcessTime = lastProcessTime;
    }

    public int getRetryTimes() {
        return retryTimes.get();
    }

    public void increment() {
        retryTimes.incrementAndGet();
    }

}
